package com.design3.graduatethesissys.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.design3.graduatethesissys.domain.Message;
import com.design3.graduatethesissys.service.impl.MessageServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class ChatController {
	
    @Autowired
    MessageServiceImpl messageService;
    //初始展示历史信息
    @ResponseBody
    @RequestMapping("Student/init")
    public List<Message> MessageStudentInit(Authentication authentication,@RequestParam("yourId")String yourid){
    	System.out.println(yourid);
        Map<String,Object> map = JSONObject.parseObject(JSON.toJSONString(authentication.getPrincipal()));
        String myid = (String) map.get("username");
        System.out.println(myid);
        System.out.println(JSON.toJSONString(messageService.getMessageByFromId(myid,yourid)));
        return messageService.getMessageByFromId(myid,yourid);
    }
    
    @ResponseBody
    @RequestMapping("Teacher/init")
    public List<Message> MessageTeacherInit(Authentication authentication,@RequestParam("yourId")String yourid){
        Map<String,Object> map = JSONObject.parseObject(JSON.toJSONString(authentication.getPrincipal()));
        String myid = (String) map.get("username");
        return messageService.getMessageByFromId(myid,yourid);
    }
    
    //查询到关于自己的所有的新消息
    @ResponseBody
    @RequestMapping("/newMessage")
    public List<Message> subjectSearch(Authentication authentication){
        Map<String,Object> map = JSONObject.parseObject(JSON.toJSONString(authentication.getPrincipal()));
        String myid = (String) map.get("username");
        return  messageService.getMyNewMessage(myid);
    }
}
